package com.wanfeng.oa.controller;

import com.wanfeng.common.JwtHelper;
import com.wanfeng.common.Result;
import com.wanfeng.common.exception.WanfengException;
import com.wanfeng.common.utils.MD5;
import com.wanfeng.model.system.SysUser;
import com.wanfeng.oa.service.SysMenuService;
import com.wanfeng.oa.service.SysUserService;
import com.wanfeng.vo.system.LoginVo;
import com.wanfeng.vo.system.RouterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 后台登录登出
 * </p>
 */
@Api(tags = "后台登录管理")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;
    //权限的service
    @Autowired
    private SysMenuService sysMenuService;
        /**
         * 登录
         * @return
         */
        @ApiOperation(value = "登录")
        @PostMapping("login")
        public Result login(@RequestBody LoginVo loginVo) {

//            Map<String, Object> map = new HashMap<>();
//            map.put("token", "admin");
//            return Result.ok(map);
//            改造
            SysUser sysUser = sysUserService.getByUsername(loginVo.getUsername());
            if(null == sysUser) {
                throw new WanfengException(201,"用户不存在");
            }
            if(!MD5.encrypt(loginVo.getPassword()).equals(sysUser.getPassword())) {
                throw new WanfengException(201,"密码错误");
            }
            if(sysUser.getStatus().intValue() == 0) {
                throw new WanfengException(201,"用户被禁用");
            }
            Map<String, Object> map = new HashMap<>();
            map.put("token", JwtHelper.createToken(sysUser.getId(), sysUser.getUsername()));
            return Result.ok(map);
        }


        /**
         * 根据token获取用户信息
         * 菜单权限  按钮权限
         * @return
         */
        @GetMapping("info")
        public Result info(HttpServletRequest request) {
            //从header获取token
            String token = request.getHeader("token");
            Long userId = JwtHelper.getUserId(token);
            String username = JwtHelper.getUsername(token);
            if(userId==null||username==null){
                return Result.ok(null);
            }
            //根据用户id获取用户的菜单
           List<RouterVo>  routerVos=sysMenuService.getRoutersByUserId(userId);
            //获取用户的按钮权限
            List<String> buttonPermsList=sysMenuService.getButtonPerms(userId);

            //获取用户的按钮权限
            Map<String, Object> map = new HashMap<>();
            map.put("roles","[admin]");
            map.put("name","admin");
            map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
            //路由
            map.put("routers", routerVos);
            //路由下边的权限
            map.put("buttons", buttonPermsList);
            //当前权限控制使用不到，我们暂时忽略
            map.put("roles",  new HashSet<>());
            return Result.ok(map);

            // map.put("roles","[admin]");
//            map.put("name","admin");
//            map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
            //修改，返回的是这个
        }
        /**
         * 退出
         * @return
         */
        @PostMapping("logout")
        public Result logout(){
            return Result.ok();
        }


}
